package com.sumover.Service.impl;

import com.sumover.DAO.UserDetailMapper;
import com.sumover.DAO.UserMapper;
import com.sumover.POJO.User;
import com.sumover.POJO.UserDetail;
import com.sumover.Service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserDetailMapper userDetailMapper;

    @Override
    public User checkUserExist(User user) {
        User result = userMapper.queryUser(user.getUsername(), user.getPassword());
        return result;
    }

    @Override
    public void addNewUser(String username, String password) {
        userMapper.addNewUser(username, password);
        User user = userMapper.queryUser(username, password);
        UserDetail userDetail = new UserDetail();
        userDetail.setUid(user.getUid());
        userDetailMapper.insert(userDetail);

    }

    @Override
    public boolean checkUserExist(String username, String password) {
        User queryResult = userMapper.queryUser(username, password);
        return queryResult != null;
    }

    @Override
    public List<User> queryAllUser() {
        return userMapper.selectAll();
    }

    @Override
    public void updateUserName(User user, String updateName) {
        user.setUsername(updateName);
        userMapper.updateByPrimaryKey(user);
    }

    @Override
    public boolean checkUsernameRepeat(String updateName) {
        List<User> userList = userMapper.selectByUsername(updateName);
        return !userList.isEmpty();
    }

    @Override
    public void updateUserPassword(User user, String password) {
        user.setPassword(password);
        userMapper.updateByPrimaryKey(user);
    }

    @Override
    public UserDetail getUserDetail(User queryUser) {
        return userDetailMapper.getDetailByUser(queryUser);
    }

    @Override
    public void updateUserDetail(UserDetail userDetail) {
        userDetailMapper.updateByPrimaryKey(userDetail);
    }
}
